// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) definits fieldsfirst ansi space safe 
// Source File Name:   Polygon.java

package jones.rigid;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;

public class Polygon
{

    protected double x[] = null;
    protected double y[] = null;
    protected int n = 0;
    private double minX = 0;
    private double maxX = 0;
    private double minY = 0;
    private double maxY = 0;
    private Point2D p = null;

    public Polygon()
    {
        p = ((Point2D) (new java.awt.geom.Point2D.Double()));
        x = new double[4];
        y = new double[4];
        n = 0;
        calculateBounds();
    }

    public Polygon(double x[], double y[], int n)
    {
        p = ((Point2D) (new java.awt.geom.Point2D.Double()));
        if (n > x.length || n > y.length || n < 0)
        {
            throw new ArrayIndexOutOfBoundsException();
        } else
        {
            this.n = n;
            this.x = new double[n];
            this.y = new double[n];
            System.arraycopy(((Object) (x)), 0, ((Object) (this.x)), 0, n);
            System.arraycopy(((Object) (y)), 0, ((Object) (this.y)), 0, n);
            calculateBounds();
            return;
        }
    }

    public double[] getX()
    {
        double x[] = new double[n];
        System.arraycopy(((Object) (this.x)), 0, ((Object) (x)), 0, n);
        return x;
    }

    public double[] getY()
    {
        double y[] = new double[n];
        System.arraycopy(((Object) (this.y)), 0, ((Object) (y)), 0, n);
        return y;
    }

    public double getX(int i)
    {
        return x[i];
    }

    public double getY(int i)
    {
        return y[i];
    }

    public int getN()
    {
        return n;
    }

    public double getMinX()
    {
        return minX;
    }

    public double getMinY()
    {
        return minY;
    }

    public double getMaxX()
    {
        return maxX;
    }

    public double getMaxY()
    {
        return maxY;
    }

    public boolean intersectsBounds(Polygon p)
    {
        return intersectsBounds(this, p);
    }

    public static boolean intersectsBounds(Polygon p1, Polygon p2)
    {
        return p1.minX < p2.maxX && p1.maxX > p2.minX && p1.minY < p2.maxY && p1.maxY > p2.minY;
    }

    protected void calculateBounds()
    {
        if (n == 0)
        {
            minX = 0.0D;
            minY = 0.0D;
            maxX = 0.0D;
            maxY = 0.0D;
            return;
        }
        minX = (1.0D / 0.0D);
        minY = (1.0D / 0.0D);
        maxX = (-1.0D / 0.0D);
        maxY = (-1.0D / 0.0D);
        for (int i = 0; i < n; i++)
        {
            double X = x[i];
            minX = minX > X ? X : minX;
            maxX = maxX < X ? X : maxX;
            double Y = y[i];
            minY = minY > Y ? Y : minY;
            maxY = maxY < Y ? Y : maxY;
        }

    }

    public boolean equals(Object o)
    {
        if (o == this)
            return true;
        if (!(o instanceof Polygon))
            return false;
        Polygon p = (Polygon)o;
        for (int i = 0; i < n; i++)
            if (x[i] != p.x[i] || y[i] != p.y[i])
                return false;

        return n == p.n && minX == p.minX && minY == p.minY && maxX == p.maxX && maxY == p.maxY;
    }

    public int hashCode()
    {
        int result = 17;
        result = 37 * result + n;
        result = 37 * result + hashDouble(minX);
        result = 37 * result + hashDouble(minY);
        result = 37 * result + hashDouble(maxX);
        result = 37 * result + hashDouble(maxY);
        for (int i = 0; i < n; i++)
        {
            result = 37 * result + hashDouble(x[i]);
            result = 37 * result + hashDouble(y[i]);
        }

        return result;
    }

    private int hashDouble(double d)
    {
        long l = java.lang.Double.doubleToLongBits(d);
        return (int)(l ^ l >>> 32);
    }

    public void reset()
    {
        n = 0;
        minX = 0.0D;
        minY = 0.0D;
        maxX = 0.0D;
        maxY = 0.0D;
    }

    public void setPolygon(Polygon p)
    {
        if (p.n > x.length)
            x = new double[p.n];
        if (p.n > y.length)
            y = new double[p.n];
        System.arraycopy(((Object) (p.x)), 0, ((Object) (x)), 0, p.n);
        System.arraycopy(((Object) (p.y)), 0, ((Object) (y)), 0, p.n);
        n = p.n;
        minX = p.minX;
        maxX = p.maxX;
        minY = p.minY;
        maxY = p.maxY;
    }

    public void setPolygon(double x[], double y[], int n)
    {
        if (n > x.length || n > y.length || n < 0)
            throw new ArrayIndexOutOfBoundsException();
        if (n > this.x.length)
            this.x = new double[n];
        if (n > this.y.length)
            this.y = new double[n];
        System.arraycopy(((Object) (x)), 0, ((Object) (this.x)), 0, n);
        System.arraycopy(((Object) (y)), 0, ((Object) (this.y)), 0, n);
        this.n = n;
        calculateBounds();
    }

    public void transform(AffineTransform transform)
    {
        for (int i = 0; i < n; i++)
        {
            p.setLocation(x[i], y[i]);
            transform.transform(p, p);
            x[i] = p.getX();
            y[i] = p.getY();
        }

        calculateBounds();
    }

    public String toString()
    {
        String s = new String();
        if (n > 0)
        {
            s = s + "[";
            for (int i = 0; i < n - 1; i++)
                s = s + "(" + x[i] + ", " + y[i] + "), ";

            s = s + "(" + x[n - 1] + ", " + y[n - 1] + ")]\n";
        }
        s = s + "n=" + n + " minX=" + minX + " minY=" + minY + " maxX=" + maxX + " maxY=" + maxY + "\n";
        return s;
    }
}
